{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# nvImageCodec batch processing examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import os.path\n",
    "from matplotlib import pyplot as plt\n",
    "from nvidia import nvimgcodec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setting resource folder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "resources_dir = os.getenv(\"PYNVIMGCODEC_EXAMPLES_RESOURCES_DIR\", \"../assets/images/\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_paths = [resources_dir + \"cat-1046544_640.jp2\", resources_dir + \"tabby_tiger_cat.jpg\",\n",
    "               resources_dir + \"Weimaraner.bmp\"]\n",
    "batch_size = len(image_paths)\n",
    "\n",
    "data_list = []\n",
    "for p in image_paths:\n",
    "    with open(p, 'rb') as in_file:\n",
    "        data = in_file.read()\n",
    "        data_list.append(data)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create decoder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dec = nvimgcodec.Decoder()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Decode data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_list = dec.decode(data_list)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Decode files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_list = dec.read(image_paths)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Show decoded images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for img in image_list:\n",
    "    print(img.__cuda_array_interface__)\n",
    "    plt.figure()\n",
    "    plt.imshow(img.cpu())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "enc = nvimgcodec.Encoder()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "encoded_imgs = enc.encode(image_list, \".jpg\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write data to file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 0\n",
    "for enc_img in encoded_imgs:\n",
    "    out_file_name = os.path.splitext(os.path.basename(image_paths[i]))[0]+ \"_out.jpg\"\n",
    "    with open(out_file_name, \"wb\") as out_file:\n",
    "        out_file.write(enc_img)\n",
    "    i+=1"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Encode and write images to file in one write function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "out_file_names = [os.path.splitext(os.path.basename(p))[0] + \"_out.jpg\" for p in image_paths]\n",
    "enc.write(out_file_names, image_list, \".jpg\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "if we skip codec argument, files will be encoded to codec deducted based on file extension"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "enc.write(out_file_names, image_list)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Decode all files in directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import glob\n",
    "import cupy as cp\n",
    "image_paths = glob.glob(resources_dir + '*.*')\n",
    "batch_size = 5\n",
    "file_name_batches = [image_paths[i: i + batch_size] for i in range(0, len(image_paths), batch_size)] \n",
    "for file_name_batch in file_name_batches:\n",
    "    #Alternatively, load data from each file and pass data_list to decode\n",
    "    \"\"\"data_list = []\n",
    "    for path in file_name_batch:\n",
    "        with open(path, \"rb\") as in_file:\n",
    "            data_list.append(in_file.read())\n",
    "    image_list = dec.decode(data_list)\"\"\"\n",
    "\n",
    "    image_list = dec.read(file_name_batch)\n",
    "\n",
    "    for img in image_list:\n",
    "        h_img = img.cpu()\n",
    "        plt.figure()\n",
    "        plt.imshow(h_img)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
